package com.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.pojo.*;
import com.util.*;

public class AdminDao {

	public int tabCount;
	private String TableName = "t_admin";
	private Connection conn = null;
	private java.sql.ResultSet rs = null;
	java.sql.PreparedStatement pstmt = null;

	// 关闭数据库连接
	public void closeAll() {
		DbUtil.close(pstmt, rs, conn);
	}

	// 登录
	public int checkPwd(String userCode, String password) {
		int flag = -1;
		try {
			if (conn == null)
				conn = DbUtil.getConnection();
			String sql = "select * from " + TableName
					+ " where name=? and pwd=? ";
			Log.write(sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userCode);
			pstmt.setString(2, password);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				//flag = rs.getInt("cityId");
				flag = 1;
			}
		} catch (Exception e) {
			Log.write(e);
		}
		return flag;
	}

	// 登录
	public AdminPojo checkPwd1(String userCode, String password) {
		AdminPojo admin = new AdminPojo();
		try {
			if (conn == null)
				conn = DbUtil.getConnection();
			String sql = "select * from " + TableName
					+ " where name=? and pwd=? ";
			Log.write(sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userCode);
			pstmt.setString(2, password);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				admin.setName(rs.getString("name"));
			}
		} catch (Exception e) {
			Log.write(e);
		}
		return admin;
	}

	// 修改密码
	public boolean updatePwd(String userCode, String password,
			String newPassword) {
		if (conn == null)
			conn = DbUtil.getConnection();
		boolean flag = false;
		try {
			if (this.checkPwd(userCode, password) >= 0) {
				pstmt = conn.prepareStatement("update " + TableName
						+ " set pwd=? where name=? and pwd=?");
				pstmt.setString(1, newPassword);
				pstmt.setString(2, userCode);
				pstmt.setString(3, password);
				pstmt.execute();
				flag = true;
			}
		} catch (Exception ex) {
			Log.write(ex);
		}
		return flag;
	}

	public int delAdmin(String sWhere) {
		int result = -1;
		try {
			if (conn == null)
				conn = DbUtil.getConnection();
			// 插入新的管理员
			pstmt = conn.prepareStatement("delete from " + TableName + sWhere);
			pstmt.execute();
			result = 0;
		} catch (Exception ex) {
			Log.write(ex);
		}
		return result;
	}

	public int editAdmin(AdminPojo Admin) {
		int result = -1;
		try {
			if (conn == null)
				conn = DbUtil.getConnection();
			// 插入新的管理员
			if (Admin.getAdminId() == 0) {
				String sql = " select * from " + TableName + " where name='"
						+ Admin.getName() + "'";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if (rs.next()) {
					result = 1;
				} else {
					sql = "insert into " + TableName
							+ "(name,pwd) values(?,?);";
					Log.write("insertsql:" + sql);
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, Admin.getName());
					pstmt.setString(2, Admin.getPwd());
					pstmt.execute();
					result = 0;
				}
			} else {
				pstmt = conn.prepareStatement("update " + TableName
						+ " set pwd = ?,name = ? where adminId=?");
				pstmt.setString(1, Admin.getPwd());
				pstmt.setString(2, Admin.getName());
				pstmt.setInt(3, Admin.getAdminId());
				pstmt.execute();
				result = 0;
			}
		} catch (Exception ex) {
			Log.write(ex);
		}
		return result;
	}

	private void getCount(String sWhere) {
		tabCount = 0;
		if (conn == null)
			conn = DbUtil.getConnection();
		try {
			pstmt = conn.prepareStatement("select count(*) as count from "
					+ TableName + " " + sWhere);
			rs = pstmt.executeQuery();
			if (rs.next())
				tabCount = rs.getInt("count");
		} catch (Exception ex) {
			ex.fillInStackTrace();
		}
	}

	// 查询管理员
	public List<AdminPojo> findByPageApp(int start, int number, String sWhere) {
		List<AdminPojo> list = new ArrayList<AdminPojo>();
		String sql = "select * from " + TableName + " " + sWhere
				+ " order by adminId desc limit " + number * (start - 1) + ","
				+ number + ";";
		Log.write(sql);
		try {
			if (conn == null)
				conn = DbUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				AdminPojo Admin = new AdminPojo();
				Admin.setId(rs.getInt("adminId"));
				Admin.setName(rs.getString("name"));
				Admin.setPwd(rs.getString("pwd"));
				Admin.setJcyName(rs.getString("jcyName"));
				Admin.setJcyTel(rs.getString("jcyTel"));
				Admin.setJcyAddress(rs.getString("jcyAddress"));
				list.add(Admin);
			}
			getCount(sWhere);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return list;
	}
	
	// 得到检察院信息
	public AdminPojo findByAdminName(String adminName) {
		AdminPojo Admin = new AdminPojo();
		String sql = "select * from " + TableName + " where name='" + adminName + "';";
		Log.write(sql);
		try {
			if (conn == null)
				conn = DbUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Admin.setId(rs.getInt("adminId"));
				Admin.setJcyName(rs.getString("jcyName"));
				Admin.setJcyTel(rs.getString("jcyTel"));
				Admin.setJcyAddress(rs.getString("jcyAddress"));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return Admin;
	}
}
